package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.DeleteSenderData;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Demon
 * @date 24/5/2024  下午7:31
 * 删除发送设备
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class DeleteSender implements CompleteEntityScheduleActions {
	
	@Override
	public ByteString objectConvertBytes() {
		final CompleteInstruction.DeleteSender.InputMsg.Builder inputMsgBuilder =
				CompleteInstruction.DeleteSender.InputMsg.newBuilder();
		DeleteSender.InputMsg thisInput = this.getInputMsg();
		List<DeleteSenderData> data = thisInput.getData();
		List<CompleteEntity.DeleteSenderData> collect = data.stream().map(DeleteSenderData::toProto).collect(Collectors.toList());
		inputMsgBuilder.addAllData(collect);
		
		final CompleteInstruction.DeleteSender.Builder builder =
				CompleteInstruction.DeleteSender.newBuilder();
		builder.setInput(inputMsgBuilder);
		return builder.build().toByteString();
	}
	
	@Override
	public DeleteSender byteConvertObject(ByteString bytes) {
		CompleteInstruction.DeleteSender deleteSender = null;
		try {
			deleteSender = CompleteInstruction.DeleteSender.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		if (Objects.isNull(deleteSender)) {
			throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
		}
		DeleteSender.OutputMsg outputMsg = new DeleteSender.OutputMsg();
		List<CompleteEntity.DeleteSenderData> dataList = deleteSender.getOutput().getDataList();
		List<DeleteSenderData> collect = dataList.stream().map(e -> new DeleteSenderData().formProto(e)).collect(Collectors.toList());
		outputMsg.setUuid(collect);
		outputMsg.setResult(deleteSender.getOutput().getResult());
		this.setOutputMsg(outputMsg);
		return this;
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class InputMsg implements Serializable {
		/**
		 * 要删除的发送设备信息
		 */
		private List<DeleteSenderData> data;
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class OutputMsg implements Serializable {
		/**
		 * 返回删除的发送设备信息
		 */
		private List<DeleteSenderData> uuid;
		
		/**
		 * 是否删除成功
		 */
		private boolean result;
	}
	
	private InputMsg inputMsg;
	private OutputMsg outputMsg;
	
}
